package com.example.empleemanagesystem.controller;

import com.example.empleemanagesystem.entity.Attendance;
import com.example.empleemanagesystem.entity.Employee;
import com.example.empleemanagesystem.entity.SalaryManage;
import com.example.empleemanagesystem.service.AttendanceService;
import com.example.empleemanagesystem.service.IEmployeeService;
import com.example.empleemanagesystem.service.SalaryManageService;
import com.example.empleemanagesystem.vo.AttendanceVO;
import com.example.empleemanagesystem.vo.SalaryManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employeeSalary")
public class EmployeeSalaryController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private SalaryManageService salaryManageService;

    @Autowired
    private AttendanceService attendanceService;

    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody Employee employee) {
        boolean result = employeeService.saveOrUpdate(employee);
        if (result) {
            SalaryManageVO salaryManageVO = new SalaryManageVO();
            salaryManageVO.setNumber(employee.getNumber().toString());
            salaryManageVO.setName(employee.getName());
            salaryManageVO.setBaseSalary(employee.getSalary().doubleValue());
            salaryManageVO.setBonus(0.0);
            salaryManageVO.setDeduction(0.0);
            salaryManageService.create(salaryManageVO);

            Attendance attendance = new Attendance();
            attendance.setNumber(employee.getNumber().toString());
            attendance.setName(employee.getName());
            attendance.setArrive_late_times(0);
            attendance.setLeave_early_times(0);
            attendanceService.create(new AttendanceVO(attendance.getId(), attendance.getNumber(), attendance.getName(), attendance.getArrive_late_times(), attendance.getLeave_early_times()));
        }
        return result;
    }

    @DeleteMapping("/delete/{number}")
    public boolean deleteEmployeeByNumber(@PathVariable String number) {
        boolean result = employeeService.removeByNumber(number);
        if (result) {
            salaryManageService.getByNumber(number).ifPresent(salaryManage -> {
                salaryManageService.deleteById(salaryManage.getId());
            });

            attendanceService.getByNumber(number).ifPresent(attendance -> {
                attendanceService.deleteById(attendance.getId());
            });
        }
        return result;
    }

    @PutMapping("/updateSalary/{id}")
    public boolean updateSalary(@PathVariable Integer id, @RequestParam Integer salary) {
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            employee.setSalary(salary);
            boolean result = employeeService.updateById(employee);
            if (result) {
                salaryManageService.getByNumber(employee.getNumber().toString()).ifPresent(salaryManage -> {
                    salaryManage.setBaseSalary(salary.doubleValue());
                    salaryManageService.update(salaryManage);
                });
            }
            return result;
        }
        return false;
    }
}